package cn.fbs.controller;

import cn.fbs.model.TLabelGroupInfo;
import cn.fbs.model.TLabelGroupInfoAdd;
import cn.fbs.model.TLabelGroupInfoUpdate;
import cn.fbs.param.StrParam;
import cn.fbs.service.LabelGroupService;
import cn.fbs.util.Result;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "标签组管理")
@RestController
@RequestMapping("labelGroup")
public class LabelGroupController {
    @Autowired
    private LabelGroupService labelGroupService;

    @ApiOperation("查询所有的标签组")
    @GetMapping("findAll")
    public List<TLabelGroupInfo> findAll() {
        return labelGroupService.findAll();
    }

    @ApiOperation("新增标签组")
    @PostMapping("add")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "name",value = "名称",required = true),
//            @ApiImplicitParam(name = "isInherent",value = "是否固有标签组1是 0否",required = true)
//    })
    public Result insert(@RequestBody @ApiParam("标签实体") TLabelGroupInfoAdd tLabelGroupInfoAdd) {
        tLabelGroupInfoAdd.setName(tLabelGroupInfoAdd.getName().trim());
        TLabelGroupInfo tLabelGroupInfo = new TLabelGroupInfo();
        BeanUtils.copyProperties(tLabelGroupInfoAdd,tLabelGroupInfo);

        Result result = new Result();
        if(StringUtils.isBlank(tLabelGroupInfo.getName())){
            result.setCode(99);result.setMsg("标签组名称未传值");
            return result;
        }
        result=labelGroupService.insert(tLabelGroupInfo);
        return result;
    }

    @ApiOperation("更新标签组")
    @PostMapping("update")
    public Result update(@RequestBody @ApiParam("标签实体") TLabelGroupInfoUpdate tLabelGroupInfoUpdate) {
        tLabelGroupInfoUpdate.setName(tLabelGroupInfoUpdate.getName().trim());
        String id = tLabelGroupInfoUpdate.getId();
        TLabelGroupInfo labelGroupInfo =  labelGroupService.getGroupInfo(id);
        labelGroupInfo.setName(tLabelGroupInfoUpdate.getName());

        Result result = new Result();
        if(labelGroupInfo.getName()!=null&&labelGroupInfo.getName().contains(" ")){
            result.setCode(99);result.setMsg("标签组名称不可含有空格");
            return result;
        }
        if(labelGroupInfo.getIsInherent() == 1){
            result.setCode(99);result.setMsg("固有标签组内容不可修改");
            return result;
        }
        return labelGroupService.update(labelGroupInfo);

    }

    @ApiOperation("根据id删除标签组")
    @PostMapping("delete")
    public Result delete(@RequestBody StrParam params) {
        Result result = new Result();
        try {
            String msg=labelGroupService.delete(params.getId());
            result.setMsg(msg);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
            result.setCode(500);
        }
        return result;
    }


}
